package com.cht.commonUtil.todolist;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cht.commonUtil.quartz.chkin.ReviewQuartChkinApply;
import com.cht.commonUtil.structure.B1I1S1;
import com.cht.commonUtil.structure.ChkInRecord;
import com.cht.commonUtil.structure.I1S2;
import com.cht.commonUtil.structure.ISR;
import com.cht.commonUtil.structure.Obj4Tester2Dep;
import com.cht.commonUtil.webInit.SP;
import com.cht.commonUtil.widgets.Widgets;
import com.google.gson.Gson;

public class IsComplete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding( SP.UIcodeSet );
		response.setCharacterEncoding( SP.UIcodeSet );
		response.setContentType("application/text");
		
		int itemType = Integer.parseInt( request.getParameter("itemType") );	//主檔的類型
		int masterId = Integer.parseInt( request.getParameter("masterId") );	//主檔ID
		ReviewQuartChkinApply rqca = new ReviewQuartChkinApply();
		String sql = "";
		
		Object obj = new Object();
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = Widgets.getConn();
			
			switch (itemType) {
			
			case 2:	//審核繳交申請
				sql="SELECT creatorId,svnOriPath,subsysId,isUrgent,svnMRurl,svnOLurl,svnMRusr,svnOLusr,svnMRver,svnOLver FROM chkinmain WHERE chkInMainId=? AND chkInStatusId=1";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				ChkInRecord cir = new ChkInRecord();
				rs.next();
				cir.setSubsysId( rs.getString("subsysId") );						//子系統
				cir.setSvnIP( rs.getString("svnOLurl") );						//SVN AP OL的位址
				cir.setSvnID( rs.getString("svnOLusr") );	  					//SVN AP OL的帳號
				cir.setReversion( rs.getInt("svnOLver") );	                        //SVN AP OL的版次
				cir.setApplier( rs.getString("creatorId") );						//繳交人員
				cir.setSvnPath( rs.getString("svnOriPath") );	//提出繳交申請時，所點選的資料夾
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				
				
				//本次繳交所綁定的需求
				List<I1S2> reqs= new ArrayList<I1S2>();		
				sql="SELECT t1.reqId,t2.reqSID,t2.reqDesc FROM mpchkinreq t1, reqs t2 WHERE t1.reqId = t2.reqId AND t1.chkInMainId=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				while(rs.next()){
					I1S2 req = new I1S2();
					req.setI1( rs.getInt("reqId") );
					req.setS1( rs.getString("reqSID") );
					req.setS2( rs.getString("reqDesc") );
					reqs.add(req);
				}
				
				//取得資安檢查報告
				ISR isr = new ISR();
				List<B1I1S1> t8 = new ArrayList<B1I1S1>();
				List<Boolean> t9 = new ArrayList<Boolean>();
				sql = "SELECT stn,isQualified,comments FROM isr8 WHERE chkInId=? ORDER BY stn";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				//存入資安檢查報告
				while( rs.next() ) {
					B1I1S1 a = new B1I1S1();
					a.setI1( rs.getInt("stn") );
					a.setB1( "t".equalsIgnoreCase(rs.getString("isQualified")) );
					a.setS1( rs.getString("comments") );
					t8.add(a);
				}
				sql = "SELECT isQualified FROM isr9 WHERE chkInId=? ORDER BY stn";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				//存入資安檢查報告
				while( rs.next() ) {
					t9.add( "t".equalsIgnoreCase(rs.getString("isQualified")) );
				}
				isr.setT8(t8);
				isr.setT9(t9);
				cir.setIsr(isr);
				cir.setReqs(reqs);
				obj = cir;
				break;
			case 3:	//等待部署
				sql="SELECT t1.exeVer,t2.subsysDesc,t3.empName,t1.cDT,t1.isDailyBuild,t1.subsysId FROM chkinmain t1,subsyses t2,emps t3 WHERE chkinmainid=? AND t1.subsysId=t2.subsysId AND t1.creatorId=t3.empId";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				Obj4Tester2Dep wait2Dep = new Obj4Tester2Dep(); 
				rs.next();
				wait2Dep.setChkInId(masterId);
				wait2Dep.setSubsysId( rs.getString("subsysId") );						//子系統
				wait2Dep.setSubsysDesc( rs.getString("subsysDesc") );
				wait2Dep.setcDT( Widgets.changeTimeFormat(rs.getTimestamp("cDT")));
				wait2Dep.setCommitter( rs.getString("empName") );
				wait2Dep.setOL( "f".equalsIgnoreCase( rs.getString("isDailyBuild") ) );
				wait2Dep.setExeVer( rs.getInt("exeVer") );
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				
				sql = "SELECT t1.reqId,t1.reqSID,t1.reqDesc FROM reqs t1,mpchkinreq t2 WHERE t1.reqId=t2.reqId AND t2.chkInMainId=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				
				List<I1S2> reqs4Dep = new ArrayList<I1S2>();
				while( rs.next() ) {
					I1S2 req = new I1S2();
					req.setI1( rs.getInt("reqId") );
					req.setS1( rs.getString("reqSID") );
					req.setS2( rs.getString("reqDesc") );
					reqs4Dep.add(req);
				}
				wait2Dep.setReqs(reqs4Dep);
				
				List<String> file4Dep = new ArrayList<String>();
				sql = "SELECT modId FROM objectlist WHERE subsysId=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, wait2Dep.getSubsysId() );
				rs = ps.executeQuery();
				while( rs.next() ) {
					file4Dep.add( rs.getString("modId") );
				}
				wait2Dep.setDepFiles( file4Dep );
				obj = wait2Dep;
				break;
			case 4:	//審核排程繳交原始碼
				sql="SELECT t4.empName,t1.quartzJobTypeId,t2.quartzJobTypeDesc,t1.cronString,t1.cronStringDesc,t1.creator,t1.cDT,t3.subsysDesc,t1.subsysId,t1.quartzId,t1.status FROM quartzjob t1,quartzJobType t2,subsyses t3,emps t4 WHERE t1.creator=t4.empId AND t1.subsysId=t3.subsysId AND t1.quartzJobTypeId=t2.quartzJobTypeId AND t1.quartzJobId=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				rs.next();
				rqca = new ReviewQuartChkinApply();
				rqca.setQuartzJobId(masterId);
				rqca.setEmpId( rs.getString("creator") );
				rqca.setEmpName( rs.getString("empName") );
				rqca.setcDT( Widgets.changeTimeFormat( rs.getTimestamp("cDT") ) );
				rqca.setCronString( rs.getString("cronString") );
				rqca.setQuartzJobTypeDesc(rs.getString("quartzJobTypeDesc"));
				rqca.setQuartzJobTypeId(rs.getInt("quartzJobTypeId"));
				rqca.setSubsysDesc( rs.getString("subsysDesc") );
				rqca.setSubsysId( rs.getString("subsysId") );
				obj = rqca;
				break;
			case 5:	//審核  修改排程繳交原始碼
				sql="SELECT t4.empName,t1.quartzJobTypeId,t2.quartzJobTypeDesc,t1.cronString,t1.cronStringDesc,t1.creator,t1.cDT,t3.subsysDesc,t1.subsysId,t1.quartzId,t1.status,t1.originalQuartzJobId FROM quartzjob t1,quartzJobType t2,subsyses t3,emps t4 WHERE t1.creator=t4.empId AND t1.subsysId=t3.subsysId AND t1.quartzJobTypeId=t2.quartzJobTypeId AND t1.quartzJobId=?";
				ps = conn.prepareStatement(sql);
				ps.setInt(1, masterId);
				rs = ps.executeQuery();
				rs.next();
				rqca = new ReviewQuartChkinApply();
				rqca.setQuartzJobId(masterId);
				rqca.setOriginalQuartzJobId( rs.getInt("originalQuartzJobId") );
				rqca.setEmpId( rs.getString("creator") );
				rqca.setEmpName( rs.getString("empName") );
				rqca.setcDT( Widgets.changeTimeFormat( rs.getTimestamp("cDT") ) );
				rqca.setCronString( rs.getString("cronString") );
				rqca.setQuartzJobTypeDesc(rs.getString("quartzJobTypeDesc"));
				rqca.setQuartzJobTypeId(rs.getInt("quartzJobTypeId"));
				rqca.setSubsysDesc( rs.getString("subsysDesc") );
				rqca.setSubsysId( rs.getString("subsysId") );
				obj = rqca;
				break;
			}
		} catch (Exception e) {
			Widgets.errLog(e);
		} finally {
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		response.getWriter().write(new Gson().toJson(obj));
	
	}

}
